package cn.tianyigps.tianyigpsaddress.assgin;

import cn.tianyigps.tianyigpsaddress.RestService;
import cn.tianyigps.tianyigpsaddress.bean.EquipmentDTO;
import cn.tianyigps.tianyigpsaddress.bean.Location;
import cn.tianyigps.tianyigpsaddress.dao.EquipmentDao;
import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpskafka.task.manger.TaskManger;
import cn.tianyigps.tianyigpspublic.bean.EquipmentModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created with IDEA
 *
 * @description: 中文赋值
 * @author: cui.guibin
 * @create: 2019/3/20 14:47
 **/
@Component
public class AssginAddress {

    @Autowired
    private EquipmentDao equipmentDao;
    @Autowired
    private RestService restService;
    @Autowired
    private TaskManger taskManger;

    public void dealTask(String equipmentId, Date start, Date end, String filter, String mapType, Task task) {

        List<EquipmentModel> equipmentCheckModels = equipmentDao.listEquipmentCheckModelById(equipmentId, start, end, "equipmentCheckModel");
        Map<String, Object> map = new HashMap<>();
        map.put("mapType",mapType.toLowerCase());
        map.put("filter",filter);
        List<EquipmentDTO> equipmentDTOS;
        List<Location> locations = new ArrayList<>();
        if (!Optional.ofNullable(equipmentCheckModels).isPresent() || equipmentCheckModels.isEmpty())
            equipmentDTOS = new ArrayList<>();
        else
            equipmentDTOS = AssginUtil.assginEquipment(equipmentCheckModels, map);

        for (EquipmentDTO equipmentDTO : equipmentDTOS) {
            Location baiduGeocode = restService.getBaiduGeocode(equipmentDTO.getGpsLongitude(), equipmentDTO.getGpsLatitude());
            equipmentDTO.setAddress(baiduGeocode.getAddress());
            locations.add(baiduGeocode);
        }
        map.put("equipments",equipmentDTOS);
        map.put("total",equipmentDTOS.size());
        Task nextTask = task.getNextTask();
        Map<String, Object> params = nextTask.getParams();
        params.putAll(map);
        nextTask.setParams(params);
        nextTask.setId(task.getId());
        taskManger.distributeTask(nextTask);
    }
}
